package com.mew.common.util;

import cn.hutool.core.util.RandomUtil;
import lombok.NonNull;

import java.util.*;

/**
 * @author SK
 */
public class RandomIdUtil {
	public static final String allChar = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    public static final String letterChar = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    public static final  char[] str = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };


    public static Long getUserId(){
        return Long.parseLong(getRandomNum(0, 14));
    }


    public static long getToken(){
        int y = (int) (System.currentTimeMillis() & 99999999);
        return getRandomLong(7, y);
    }

    public static Long getRandomLong(int length, int start){
        return Long.parseLong(getRandomNum(start, length));
    }

    public static Integer getRandomInt(int length){
        return Integer.parseInt(getRandomNum(0, length));
    }

    public static String getGid(int start, int length){
        return getRandomNum(start, length == 0 ? 8 : length);
    }

    private static String getRandomNum(int start, int length){
        int i;
        int count = 0;
        length = length == 0 ? 8 : length;
        StringBuffer sb = new StringBuffer(length);
        sb.append(0 == start ?  RandomUtil.randomInt(1, 10) : start);
        while(count < length-1){
            i = RandomUtil.randomInt(0, 10);
            if (i >= 0 && i < str.length) {
                sb.append(str[i]);
                count ++;
            }
        }
        return sb.toString();
    }

    /**
     * 返回一个定长的随机字符串(只包含大小写字母、数字)
     * 
     * @param length
     *            随机字符串长度
     * @return 随机字符串
     */
    public static String generateString(int length) {
        StringBuffer sb = new StringBuffer();
        Random random = new Random();
        for (int i = 0; i < length; i++) {
            sb.append(allChar.charAt(random.nextInt(allChar.length())));
        }
        return sb.toString();
    }

    /**
     * 返回一个定长的随机纯字母字符串(只包含大小写字母)
     * 
     * @param length
     *            随机字符串长度
     * @return 随机字符串
     */
    public static String generateMixString(int length) {
        StringBuffer sb = new StringBuffer();
        Random random = new Random();
        for (int i = 0; i < length; i++) {
            sb.append(allChar.charAt(random.nextInt(letterChar.length())));
        }
        return sb.toString();
    }

    /**
     * 返回一个定长的随机纯大写字母字符串(只包含大小写字母)
     * 
     * @param length
     *            随机字符串长度
     * @return 随机字符串
     */
    public static String generateLowerString(int length) {
        return generateMixString(length).toLowerCase();
    }

    /**
     * 返回一个定长的随机纯小写字母字符串(只包含大小写字母)
     * 
     * @param length
     *            随机字符串长度
     * @return 随机字符串
     */
    public static String generateUpperString(int length) {
        return generateMixString(length).toUpperCase();
    }

    public static String getUUID(){
        return UUID.randomUUID().toString().replace("-", "").toUpperCase();
    }

    /**
     * 生成一个定长的纯0字符串
     * 
     * @param length
     *            字符串长度
     * @return 纯0字符串
     */
    public static String generateZeroString(int length) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < length; i++) {
            sb.append('0');
        }
        return sb.toString();
    }

    /**
     * 根据数字生成一个定长的字符串，长度不够前面补0
     * 
     * @param num
     *            数字
     * @param fixdlenth
     *            字符串长度
     * @return 定长的字符串
     */
    public static String toFixdLengthString(long num, int fixdlenth) {
        StringBuffer sb = new StringBuffer();
        String strNum = String.valueOf(num);
        if (fixdlenth - strNum.length() >= 0) {
            sb.append(generateZeroString(fixdlenth - strNum.length()));
        } else {
            throw new RuntimeException("将数字" + num + "转化为长度为" + fixdlenth
                    + "的字符串发生异常！");
        }
        sb.append(strNum);
        return sb.toString();
    }

    /**
     * 根据数字生成一个定长的字符串，长度不够前面补0
     * 
     * @param num
     *            数字
     * @param fixdlenth
     *            字符串长度
     * @return 定长的字符串
     */
    public static String toFixdLengthString(int num, int fixdlenth) {
        StringBuffer sb = new StringBuffer();
        String strNum = String.valueOf(num);
        if (fixdlenth - strNum.length() >= 0) {
            sb.append(generateZeroString(fixdlenth - strNum.length()));
        } else {
            throw new RuntimeException("将数字" + num + "转化为长度为" + fixdlenth
                    + "的字符串发生异常！");
        }
        sb.append(strNum);
        return sb.toString();
    }

    /**
     * 指定数值和几率配置,随机获取数值
     * 例如roundnums{0,1,2,3}  probabilitys{45,30,15,10}
     *     出      0:45%    1:30%    2:15%     3:10%
     * @param roundnums 数值数组
     * @param probabilitys 几率数组
     * @return  随机数值
     * @throws Exception 配置数组大小必须一样,几率配置和必须是100
     * @author tsl
     */
    public static int produceRateRandomNumber(int[] roundnums,int[] probabilitys){
        if(roundnums.length!=probabilitys.length){
            throw new IllegalArgumentException("数值和概率设置不匹配");
        }
        int sumprobabilitys=0;
        for(int probability:probabilitys){
            sumprobabilitys+=probability;
        }
        if(sumprobabilitys!=100){
            throw new IllegalArgumentException("百分比之和必须为100%");
        }
        int randnum = RandomUtil.randomInt(0, 100); //[0,99]
        int percentScopeMin=0;
        int percentScopeMax=0;
        for(int idx=0;idx<probabilitys.length;idx++){
            percentScopeMin=percentScopeMax;
            percentScopeMax+=probabilitys[idx];
            if(randnum>=percentScopeMin&&randnum<percentScopeMax){
                return roundnums[idx];
            }
        }
        return -1;
    }

    public static void main(String[] args) {

        for (int j=0; j<5; j++){
            int[] win = new int[7];
            for (int i=0; i<6; i++){
                win[i] = 33- RandomUtil.randomInt(1, 33);
            }
            win[6] = 16- RandomUtil.randomInt(1, 17);
            System.out.println(Arrays.toString(win));
        }
//        String noteMSG = StrUtil.format("游戏[id:{} {}] 房间:[id: {} {}] 平衡值:{} 容量池:{} {}, 请及时处理!",
//                game.getId(), game.getGameVfx(), room.getId(), room.getRoomType(), room.getStratBloodBalance(),  roomProfile.getCapacityValue(), ct == 1 ? "橙色预警" : "红色预警");
//        System.out.println(noteMSG);

//        int[] proparea = new int[5];
//        for (int i=0; i< 5; i++){
//            proparea[i] = RandomUtil.randomInt(1, 36);
//        }
//        int[] win = new int[2];
//        win[0] = RandomUtil.randomInt(1, 13);
//        win[1] = RandomUtil.randomInt(1, 13);
//
//        System.out.println(Arrays.toString(proparea) +" "+ Arrays.toString(win));

//
//        byte[] bytes = io.netty.buffer.ByteBufUtil.decodeHexDump("15D6000E1388000000C8000000000BED36C1");
//        System.out.println(bytes.length);
//        ByteBuf byteBuf = PooledByteBufAllocator.DEFAULT.directBuffer();
//        byteBuf.writeBytes(bytes);
//
//        short flag = byteBuf.readShort();
//        short len = byteBuf.readShort();
//        short cmd = byteBuf.readShort();
//        int length = len - 2;
//        int realLength = byteBuf.readableBytes();


//        System.out.println("-------333------" + byteBuf.readInt());



//        System.out.println( NetUtil.getLocalhost().getHostAddress());
//        System.out.println(MD5Encrypt.MD5("aa123456"));
//        System.out.println( NetUtil.getLocalHostName());
//        System.out.println(NetUtil.isInnerIP("192.168.0.101"));
//        System.out.println(NetUtil.ipv4ToLong("192.168.0.101"));
//        System.out.println(StrUtil.format("{}:{}:{}", "lobby", NetUtil.getLocalhost().getHostAddress(), 9898 ));

//        String xx = "XXX-1231231:22";
//        String yy = "xxx-";
//        System.out.println(StrUtil.startWith(xx, yy));
////        "LOBBY-lobby:192.168.0.101:9879"
//        String str = "200,300,400";
//        int[] ar = StrUtil.splitToInt(str, ',');
//        System.out.println(Arrays.toString(ar));
//        for (int i=0; i<200; i++){
//            System.out.println(i+"  "+i%3);
//        }

    }

    public static int bitCount(int i) {
        // HD, Figure 5-2
        i = i - ((i >>> 1) & 0x55555555);
        i = (i & 0x33333333) + ((i >>> 2) & 0x33333333);
        i = (i + (i >>> 4)) & 0x0f0f0f0f;
        i = i + (i >>> 8);
        i = i + (i >>> 16);
        return i & 0x3f;
    }


    public static void setStr(@NonNull String key){
        System.out.println(key);
    }
}
